package request

import "gitee.com/arjunxw/iothub-south-client/util"

const (
	Ok                 = "OK"
	NotFound           = "NOT_FOUND"
	Unauthorized       = "UNAUTHORIZED"
	PermissionDenied   = "PERMISSION_DENIED"
	IllegalArgument    = "ILLEGAL_ARGUMENT"
	Failed             = "FAILED"
	Occupy             = "OCCUPY"
	InvalidAccessToken = "INVALID_ACCESS_TOKEN"
)

type AuthResult struct {
	ResponseBody
	Body authResultBody `json:"body"`
}

type authResultBody struct {
	AccessToken  string      `json:"accessToken"`
	RefreshToken string      `json:"refreshToken"`
	ExpiresIn    interface{} `json:"expiresIn"`
	Scope        string      `json:"scope"`
	TokenType    string      `json:"tokenType"`
}

func (a *authResultBody) GetExpiresIn() int32 {
	return util.JsonValueToInt32(a.ExpiresIn)
}

type GwTopology struct {
	GwProductKey string                 `json:"gwProductKey"`
	GwSn         string                 `json:"gwSn"`
	States       map[string]interface{} `json:"states"`
	SubDevices   []SubDevice            `json:"subDevices"`
}

type SubDevice struct {
	SubProductKey   string                 `json:"subProductKey"`
	SubDeviceSn     string                 `json:"subDeviceSn"`
	SubDeviceName   string                 `json:"subDeviceName"`
	SubDeviceRemark string                 `json:"subDeviceRemark"`
	States          map[string]interface{} `json:"states"`
}

type ResponseBody struct {
	Code    string      `json:"code"`
	ErrCode interface{} `json:"errCode"`
	Message string      `json:"message"`
	Body    interface{} `json:"body"`
}

func (resp *ResponseBody) SetBody(body interface{}) *ResponseBody {
	resp.Body = body
	return resp
}

func (resp *ResponseBody) SetMessage(message string) *ResponseBody {
	resp.Message = message
	return resp
}

func (resp *ResponseBody) GetErrCode() int32 {
	return util.JsonValueToInt32(resp.ErrCode)
}

func BuildResponseOk() *ResponseBody {
	return &ResponseBody{Code: Ok, Message: "Success"}
}

func BuildResponseFailed() *ResponseBody {
	return &ResponseBody{Code: Failed, Message: "Failed"}
}

func BuildResponseNotFound() *ResponseBody {
	return &ResponseBody{Code: NotFound, Message: "NotFound"}
}

func BuildResponseIllegalArgument() *ResponseBody {
	return &ResponseBody{Code: IllegalArgument, Message: "IllegalArgument"}
}
